package com.example.administrator.yunniuusedcar.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.common.base.Global;
import com.example.administrator.common.base.MyApp;
import com.example.administrator.yunniuusedcar.MainActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.activity.CommodityDetailsActivity;
import com.example.administrator.yunniuusedcar.adapter.PingXing_zongHeAdapter2;
import com.example.administrator.yunniuusedcar.adapter.baseadapter.BaseAdapter;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.PingXingZH_Bean;
import com.example.administrator.yunniuusedcar.refresh.PullRefreshLayout;
import com.example.administrator.yunniuusedcar.util.Judge;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/** 平进豪车 综合
 * Created by Administrator on 2017/12/29.
 */

public class PingJingDefaultFragment extends BaseFragment{

    private int pager = 1;
    private PullRefreshLayout mPull;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView8;
    private PingXing_zongHeAdapter2 adapter;
    List<PingXingZH_Bean.Data> dataList =new ArrayList<>();
    @SuppressLint("ValidFragment")
    public PingJingDefaultFragment() {
    }


    @Override
    public int getLayoutRes() {
        return R.layout.item_recycleview2;
    }

    @Override
    public void initView() {
        swipeRefreshLayout = findView(R.id.swipelayout);
        mPull =  findView(R.id.pull);
        initRecycleV();
    }
    private void initRecycleV() {

        recyclerView8 = findView(R.id.recycler_view6);

        recyclerView8.setLayoutManager(new LinearLayoutManager(mActivity));

        //创建数据
        listDatas = new ArrayList();

//        zhongGuiCarZongHeAdapter1 = new ZhongGuiCar_zongHeAdapter(getContext(),
//                R.layout.fragment_zonghepaixu,listDatas);
        adapter = new PingXing_zongHeAdapter2(getContext());
        recyclerView8.setAdapter(adapter);


        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                dataList.clear();
                pager =1;
                initUloadMenuInfor2();
            }
        });


        mPull.setOnPullListener(new PullRefreshLayout.OnPullListener() {
            @Override
            public void onLoadMore(final PullRefreshLayout pullRefreshLayout) {
                pager = pager+1;
                initUloadMenuInfor2();
                pullRefreshLayout.setRefreshing(false);
            }
        });

/////
    }
    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int SuiJiCarID =  dataList.get(position).car_id;    //中规豪车 综合排序id

                Intent intent = new Intent(MainActivity.mactivity, CommodityDetailsActivity.class);   // 商品详情
                intent.putExtra("RanCarid",SuiJiCarID);

                MainActivity.mactivity.startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        initUloadMenuInfor2();    //// (  加载数据 )
    }

    static ArrayList listDatas = new ArrayList();

    private void initUloadMenuInfor2() {    // 加载数据
//        if(MyApp.enter_type.equals("typeCar")){
//
//            String ul = IHttpService.URL2 +IHttpService.GETBYCARTYPELIS ;    // 根据车辆类型选车   不同类型

        String ul2 = IHttpService.HOST_URL + "Car/typeCar";

            OkHttpUtils.post().url(ul2).addParams("sign",IHttpService.SignVal).addParams("page",pager+"").addParams("num","3")
                    .addParams("type","1") .addParams("type_id","7").build().execute(new StringCallback() {     //.addParams("type","1")    //综合

                @Override
                public void onError(Call call, Exception e, int id) {
                    Global.showToast("请求超时！"+ e.getMessage());
                    //      System.out.println("-----====----------A"+ e);
                }

                @Override
                public void onResponse(String response, int id) {
                    handlerData(response);
                }
            });
//        }else if(MyApp.enter_type.equals("termCar")){
//            String ul = IHttpService.URL2 +IHttpService.GETSEARCHCARLIST ;  //根据条件选车
        String ul3 = IHttpService.HOST_URL + "Car/termCar";

            OkHttpUtils.post().url(ul3).addParams("sign",IHttpService.SignVal).addParams("page",pager+"").addParams("num","3")
                    .addParams("type","1")
                    .addParams("type_id", MyApp.type_id+"")
                    .addParams("brand_id",MyApp.brand_id+"")
                    .addParams("level_id",MyApp.level_id+"")
                    .addParams("price_id",MyApp.price_id+"")
                    .build().execute(new StringCallback() {     //.addParams("type","1")   价格排序

                @Override
                public void onError(Call call, Exception e, int id) {
                    Global.showToast("中规豪车、价格排序，请求超时！"+ e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    handlerData(response);
                }
            });
        }

    /**
     * 处理data
     * @param response
     */
    private void handlerData(String response) {
        try {
            swipeRefreshLayout.setRefreshing(false);
            JSONObject jsonObject = new JSONObject(response);
            String data = jsonObject.getString("data");
            dataList .addAll(getDataList(data));
            adapter.refreshData(dataList);
            //recyclerView7.refreshComplete();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private List<PingXingZH_Bean.Data> getDataList(String data_str) {
        if (!Judge.isJson(data_str)) {
            return null;
        }

        List<PingXingZH_Bean.Data> datas = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(data_str);

            for (int i = 0; i < array.length(); i++) {
                PingXingZH_Bean.Data bean = new Gson().fromJson(array.get(i).toString(), PingXingZH_Bean.Data.class);
                datas.add(bean);
            }
            return datas;
        } catch (JSONException e) {
            return null;
        }
    }

//    private RecyclerView recyclerView7;
//    private PingXing_zongHeAdapter2 pingXingZongHeAdapter2;
//    ArrayList listDatas = new ArrayList();
//
//    @Override
//    public int getLayoutRes() {
//        return R.layout.item_recycleview2;  //fragment_zonghepaixu
//
//    }
//    @Override
//    public void initView() {
//        initUloadMenuInfor();
//        initRecycleV();
//    }
//    private void initRecycleV() {
//
//        recyclerView7 = findView(R.id.recycler_view6);
//
//        recyclerView7.setLayoutManager(new LinearLayoutManager(mActivity));
//
//        //创建数据
//        listDatas = new ArrayList();
//
////        zhongGuiCarZongHeAdapter1 = new ZhongGuiCar_zongHeAdapter(getContext(),
////                R.layout.fragment_zonghepaixu,listDatas);
//        pingXingZongHeAdapter2 = new PingXing_zongHeAdapter2(getContext(),listDatas);
//        recyclerView7.setAdapter(pingXingZongHeAdapter2);
//        initUloadMenuInfor();    //// (  加载数据 )
///////
//    }
//
//    @Override
//    public void initListener() {
//
//    }
//
//    @Override
//    public void initData() {
//
//    }
//    private void initUloadMenuInfor() {    // 加载数据
//
//        String ul = IHttpService.HOST_URL + "Car/typeCar";    // 综合排序
//
//        OkHttpUtils.post().url(ul).addParams("sign",IHttpService.SignVal).addParams("page","1").addParams("num","3")
//                .addParams("type","1").addParams("type_id","7").build().execute(new StringCallback() {     //.addParams("type","1")   综合排序
//
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                Global.showToast("请求超时！"+ e.getMessage());
//                System.out.println("-----====----------A"+ e);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                ////        Global.showToast("====="+response);
//                System.out.println("-----====-----------A1"+response);
//                Gson js = new Gson();
//                PingXingZH_Bean wzArticleInfoBean = js.fromJson(response,PingXingZH_Bean.class);
//                listDatas.add(wzArticleInfoBean);
////                ZhongGuiCar_zongHeAdapter zhongGuiCarZongHeAdapter = new ZhongGuiCar_zongHeAdapter();
////                zhongGuiCarZongHeAdapter.set
//                pingXingZongHeAdapter2.notifyDataSetChanged();
//                //     mainInfoAdapter.setDatas(listDatas);
//            }
//        });
//    }
}
